# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=netdata
pkgver=1.24.0
pkgrel=0
pkgdesc="Real-time performance monitoring"
url="https://www.netdata.cloud/"
license="GPL-3.0-or-later"
arch="all"
depends="bash"
makedepends="zlib-dev e2fsprogs-dev linux-headers libcap-dev automake autoconf
	openssl-dev libuv-dev"
pkgusers="netdata"
pkggroups="netdata"
subpackages="$pkgname-openrc $pkgname-nodejs::noarch $pkgname-python::noarch"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/netdata/netdata/archive/v$pkgver.tar.gz
	$pkgname.initd"

prepare() {
	default_prepare
	autoreconf -i
}

build() {
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-zlib \
		--with-math \
		--enable-https \
		--with-webdir=/usr/share/webapss/netdata \
		--with-user=netdata
	make
}

check() {
	msg "check"
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	chgrp -R $pkggroups "$pkgdir"/usr/share/webapss/netdata
	install -Dm755 $srcdir/$pkgname.initd \
		$pkgdir/etc/init.d/$pkgname
	install -Dm644 ./system/netdata.conf $pkgdir/etc/netdata/netdata.conf

	# cap needed for disk I/O utilization reporting
	setcap cap_dac_read_search,cap_sys_ptrace+ep \
		"$pkgdir"/usr/libexec/netdata/plugins.d/apps.plugin

	install -d -m0750 -o netdata -g netdata \
		"$pkgdir"/var/lib/netdata \
		"$pkgdir"/var/log/netdata \
		"$pkgdir"/var/cache/netdata
}

nodejs() {
	pkgdesc="$pkgdesc (nodejs support)"
	depends="$pkgname nodejs"

	mkdir -p "$subpkgdir"/etc/netdata
	mkdir -p "$subpkgdir"/usr/libexec/netdata/plugins.d
	mkdir -p "$subpkgdir"/usr/lib/netdata/conf.d

	mv $pkgdir/usr/libexec/netdata/node.d \
		$subpkgdir/usr/libexec/netdata
	mv $pkgdir/usr/libexec/netdata/plugins.d/node.d.plugin \
		$subpkgdir/usr/libexec/netdata/plugins.d
	mv $pkgdir/etc/netdata/node.d \
		$subpkgdir/etc/netdata/
	mv $pkgdir/usr/lib/netdata/conf.d/node.d.conf \
		$subpkgdir/usr/lib/netdata/conf.d
}

python() {
	pkgdesc="$pkgdesc (python3 support)"
	depends="$pkgname python3"

	mkdir -p "$subpkgdir"/etc/netdata
	mkdir -p "$subpkgdir"/usr/libexec/netdata/plugins.d
	mkdir -p "$subpkgdir"/usr/lib/netdata/conf.d

	mv $pkgdir/usr/libexec/netdata/python.d \
		"$subpkgdir"/usr/libexec/netdata
	mv $pkgdir/usr/libexec/netdata/plugins.d/python.d.plugin \
		"$subpkgdir"/usr/libexec/netdata/plugins.d
	mv $pkgdir/etc/netdata/python.d \
		$subpkgdir/etc/netdata/
	mv $pkgdir/usr/lib/netdata/conf.d/python.d \
		$subpkgdir/usr/lib/netdata/conf.d
	mv $pkgdir/usr/lib/netdata/conf.d/python.d.conf \
		$subpkgdir/usr/lib/netdata/conf.d
}

sha512sums="18276605f74520b3d55b8a67bfbee7cb2483d472becbcab7e2b1af19f08b05ffd2a83e6e8eec98733abde8ba1f552eec919e8ae59266cbec7c2c94beb4a2aeb4  netdata-1.24.0.tar.gz
5a9af8de32dba1dd982cd0b0d696b60329cf834edd581e9b39c1e4ec21f5b6eb05e224e238ffd20f3e621c48c453841c09fdaba21318c49b23b3b2e4ef04b34d  netdata.initd"
